package com.campus.wall.presenter;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.campus.wall.base.BasePresenter;
import com.campus.wall.contract.LoginContract;
import com.campus.wall.contract.MainContract;

public class MainPresenter  extends BasePresenter<MainContract.View> implements MainContract.Presenter{

    private Activity activity;
    private  final String[] permission = new String[]{
            Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    };
    public MainPresenter(Activity activity) {
        this.activity=activity;
    }

    public  boolean isPermissionGranted(){
        if(Build.VERSION.SDK_INT >= 23){
            for(int i = 0; i < permission.length;i++) {
                int checkPermission = ContextCompat.checkSelfPermission(activity,permission[i]);
                /***
                 * checkPermission返回两个值
                 * 有权限: PackageManager.PERMISSION_GRANTED
                 * 无权限: PackageManager.PERMISSION_DENIED
                 */
                if(checkPermission != PackageManager.PERMISSION_GRANTED){
                    return false;
                }
            }
            return true;
        }else{
            return true;
        }
    }
    public  boolean checkPermission(){
        if(isPermissionGranted()) {
            return true;
        } else {
            //如果没有设置过权限许可，则弹出系统的授权窗口
            ActivityCompat.requestPermissions(activity,permission,1001);
            return false;
        }
    }
}
